package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: YKH
 * Date: 2022-10-27
 * Time: 14:06
 */
@WebServlet("/index")
public class Index extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //把当前的用户信息展示到页面上
        //参数 填false, 此处是获取当前用户的登录状态, 有session则有,没有就要求用户重新登录
        HttpSession session = req.getSession(false);
        if(session == null) {
            //用户未登录, 跳转到用户登录界面
            resp.sendRedirect("login.html");
            return ;
        }
        //登录成功 展示当前用户的登录信息
        String username = (String) session.getAttribute("username");
        String password = (String) session.getAttribute("password");
        Integer visitCount = (Integer) session.getAttribute("visitCount");
        session.setAttribute("visitCount",visitCount+1);
        resp.setContentType("text/html; charset=utf8");
        resp.getWriter().write("用户名: " +username + "  密码:  "+password + "访问次数"+visitCount);
    }
}
